package com.swind.fast.common.aspect;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import com.swind.fast.common.api.vo.Result;
import com.swind.fast.modules.base.service.BaseCommonService;
import com.swind.fast.common.api.dto.SwindLogDTO;
import com.swind.fast.common.aspect.annotation.SwindAutoLog;
import com.swind.fast.common.util.SpringContextUtils;
import com.swind.fast.common.util.IPUtils;

// import org.jeecg.common.api.dto.LogDTO;
// import org.jeecg.common.api.vo.Result;
// import org.jeecg.common.aspect.annotation.AutoLog;
// import org.jeecg.common.constant.CommonConstant;
// import org.jeecg.common.constant.enums.ModuleType;
// import org.jeecg.modules.base.service.BaseCommonService;
// import org.jeecg.common.system.vo.LoginUser;
// import org.jeecg.common.util.IPUtils;
// import org.jeecg.common.util.SpringContextUtils;
// import org.jeecg.common.util.oConvertUtils;

import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 系统日志，切面处理类
 */
@Aspect
@Component
public class SwindAutoLogAspect {

    @Resource
    private BaseCommonService baseCommonService;

    @Pointcut("@annotation(com.swind.fast.common.aspect.annotation.SwindAutoLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        //保存日志
        saveSwindSysLog(point, time, result);

        return result;
    }

    private void saveSwindSysLog(ProceedingJoinPoint joinPoint, long time, Object obj) {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SwindLogDTO dto = new SwindLogDTO();
        SwindAutoLog syslog = method.getAnnotation(SwindAutoLog.class);
        if (syslog != null) {
            String logType = syslog.logType();
            String content = getSwindLogContent(obj);
            dto.setLogType(logType);
            dto.setLogContent(content);
        }

        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        dto.setIp(IPUtils.getIpAddr(request));
        dto.setCostTime(time);
        dto.setCreateTime(new Date());
        baseCommonService.addSwindLog(dto);



        // MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // Method method = signature.getMethod();

        // LogDTO dto = new LogDTO();
        // AutoLog syslog = method.getAnnotation(AutoLog.class);
        // if(syslog != null){
        //     //update-begin-author:taoyan date:
        //     String content = syslog.value();
        //     if(syslog.module()== ModuleType.ONLINE){
        //         content = getOnlineLogContent(obj, content);
        //     }
        //     //注解上的描述,操作日志内容
        //     dto.setLogType(syslog.logType());
        //     dto.setLogContent(content);
        // }

        // //请求的方法名
        // String className = joinPoint.getTarget().getClass().getName();
        // String methodName = signature.getName();
        // dto.setMethod(className + "." + methodName + "()");

        // //设置操作类型
        // if (dto.getLogType() == CommonConstant.LOG_TYPE_2) {
        //     dto.setOperateType(getOperateType(methodName, syslog.operateType()));
        // }

        // //获取request
        // HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        // //请求的参数
        // dto.setRequestParam(getReqestParams(request,joinPoint));
        // //设置IP地址
        // dto.setIp(IPUtils.getIpAddr(request));
        // //获取登录用户信息
        // LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // if(sysUser!=null){
        //     dto.setUserid(sysUser.getUsername());
        //     dto.setUsername(sysUser.getRealname());

        // }
        // //耗时
        // dto.setCostTime(time);
        // dto.setCreateTime(new Date());
        // //保存系统日志
        // baseCommonService.addLog(dto);
    }

    // /**
    //  * online日志内容拼接
    //  * @param obj
    //  * @param content
    //  * @return
    //  */
    // private String getOnlineLogContent(Object obj, String content){
    //     if (Result.class.isInstance(obj)){
    //         Result res = (Result)obj;
    //         String msg = res.getMessage();
    //         String tableName = res.getOnlTable();
    //         if(oConvertUtils.isNotEmpty(tableName)){
    //             content+=",表名:"+tableName;
    //         }
    //         if(res.isSuccess()){
    //             content+= ","+(oConvertUtils.isEmpty(msg)?"操作成功":msg);
    //         }else{
    //             content+= ","+(oConvertUtils.isEmpty(msg)?"操作失败":msg);
    //         }
    //     }
    //     return content;
    // }

    private String getSwindLogContent(Object obj) {
        String content = "";
        if (Result.class.isInstance(obj)){
            Result res = (Result) obj;
            content = res.getLogContent();
        }
        return content;
    }
}
